package com.huangguolin.cloud.role.service.impl;

import com.huangguolin.cloud.mybatis.entity.PtRoleEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PadingRstEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PagingEntity;
import com.huangguolin.cloud.mybatis.ztree.ZtreeEntity;
import com.huangguolin.cloud.role.entity.SearchRoleEntity;
import com.huangguolin.cloud.role.repository.RoleDao;
import com.huangguolin.cloud.role.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleDao roleDao;

    @Override
    public PadingRstEntity<PtRoleEntity> queryRoleListByPage(SearchRoleEntity entity, PagingEntity paging) {

        paging.dealColums(PtRoleEntity.class);
        PadingRstEntity<PtRoleEntity> ptRoleEntityPadingRstEntity = new PadingRstEntity<>();
        ptRoleEntityPadingRstEntity.setPage(paging.getPage());
        entity.deal();
        List<PtRoleEntity> list = roleDao.queryRoleListByPage(entity,paging);
        Integer total = roleDao.queryRoleTotal(entity);
        ptRoleEntityPadingRstEntity.setRawRecords(list);
        ptRoleEntityPadingRstEntity.setTotal(total);
        ptRoleEntityPadingRstEntity.putItem();
        return ptRoleEntityPadingRstEntity;
    }

    @Override
    public List<ZtreeEntity> queryMenuZtreeAll() {
        return roleDao.queryMenuZtreeAll();
    }

    @Override
    public void saveMenuRefRole(String menuIds, String roleIds) {
        String[] menuIdArray = menuIds.split(",");
        String[] roleIdArray = roleIds.split(",");
        for (String roleId:roleIdArray){
            roleDao.deleteMenuByRole(roleId);
            for(String menuId:menuIdArray){
                roleDao.insertMenuByRole(roleId,menuId);

            }
        }
    }

    @Override
    public List<ZtreeEntity> queryMenuZtreeByRole(String roleId) {
        List<ZtreeEntity> ztreeEntityList = queryMenuZtreeAll();
        List<String> menuIdList = roleDao.queryMenuByRole(roleId);
        for (ZtreeEntity entity:ztreeEntityList){
            for (String menuId:menuIdList){
                if(menuId.equals(entity.getId())){
                    entity.setChecked(true);
                    break;
                }
            }
        }
        return ztreeEntityList;
    }

    @Override
    public void addRole(String roleName, String remarks) {
       roleDao.addRole(roleName,remarks);
    }

    @Override
    public void deleteRole(String roleUuid) {
        roleDao.deleteRole(roleUuid);
    }

    @Override
    public void modifyRole(String roleUuid, String roleName, String remarks) {
        roleDao.modifyRole(roleUuid,roleName,remarks);
    }

    @Override
    public List<ZtreeEntity> getRoleAuthority(String roleUuid) {
        List<ZtreeEntity> ztreeEntityList = queryMenuZtreeAll();
        List<String> menuIdList = roleDao.getRoleAuthority(roleUuid);
        for (ZtreeEntity entity:ztreeEntityList){
            for (String menuId:menuIdList){
                if(menuId.equals(entity.getId())){
                    entity.setChecked(true);
                    break;
                }
            }
        }
        return ztreeEntityList;
    }

}
